<?php
	class Topic_tiankong extends MY_controller{

		function __construct(){
			parent::__construct(); 
		}

		function add(){
			if($_POST){
				$post_data = $this->input->post();
				// print_r($post_data['an_tiakong']);exit;
				if(empty($post_data['an_tiakong'])){
					show_message("/topic/add_tiankong", "没有任何答案");
					exit;
				}

				//处理题目HTML内容
				$sub_text = $post_data['subject_text'];
				$sub_text = cleanhtml($sub_text);//过滤img以外的html标签
				$sub_text = str_replace('&nbsp;','',$sub_text);
				
				$regex1 = '/（[?=\s]+）/';
				//把括号替换成下划线
				$sub_text = preg_replace($regex1, "____", $sub_text); 
				//重新赋值
				$post_data['subject_text'] = $sub_text;
				$an_tiakong = $post_data['an_tiakong'];
				unset($post_data['an_tiakong'],$post_data['file']);
				$this->db->insert("tk_topics", $post_data);
				$insert_id = $this->db->insert_id();;

				$tiankong_answer = array();
				foreach ($an_tiakong as $key => $answer_list) {
					if($key == 'symbol'){
						$type = 2;
					}elseif($key == 'number'){
						$type = 3;
					}elseif($key == 'calc'){
						$type = 4;
						$suanshi = make_semiangle($answer_list);
						$result_array = formula($suanshi[1]);
						$result_data = array();
						$fh = array(1 => "+", 2 => "-", 3 => "*", 4 => "/", 5 => "(", 6 => ")");
						foreach ($result_array as $key => $value) {
							if(is_numeric($value)){
								$calc_type = 1;
							}else{
								$calc_type = 2;
								$value = array_search($value, $fh);
							}
							$result_data[] = array("topic_id" => $insert_id, "value" => $value, "type" => $calc_type);
							// echo $value . '<br>';
						}
						$this->db->insert_batch("tk_topic_result_calc", $result_data);
						// header("Location:/topic/index");exit;
					}else{
						$type = 1;
					}
					foreach ($answer_list as $k => $v) {
						if($key == 'calc'){
							//如果是算式类型
							
						}
						$answer_data['topic_id'] = $insert_id;
						$answer_data['answer_text'] = $v;
						$answer_data['is_right'] = 1;
						$answer_data['type'] = $type;
						$tiankong_answer[] = $answer_data;
					}
				}
				// exit;
				$this->db->insert_batch("tk_topic_result", $tiankong_answer);
				// show_message("/topic/add_tiankong", "添加题目成功");
				header("Location:/topic/index");exit;
			}
			//获取科目
			$this->load->model('Subject_model');
			$subject = $this->Subject_model->get_subject_all();
			$this->smarty->assign("subject", $subject);

			$this->load->model('Subject_book_model');
			$book_list = $this->Subject_book_model->get_book_list();
			$letter = array('A','B','C','D');
			$this->smarty->assign("book_list", $book_list['result']);
			$chapter_list_data = $this->get_chapter_list(27);
			$this->smarty->assign("chapter_list_data", $chapter_list_data);
			$this->smarty->assign("action", '添加填空题');
			$this->smarty->display("topic/save_topic_tiankong2.html");
		}


		function get_chapter_list($subject_id){
			$this->load->model('Subject_book_model');
			$chapter_list = $this->Subject_book_model->get_chapter_list_by_book($subject_id);
			$chapter_list_data = array();
			$code = 0;
			if($chapter_list){
				foreach ($chapter_list as $key => $value) {
					if($value['parent_id'] == 0){
						$chapter_list_data[$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				foreach ($chapter_list as $key => $value) {
					if(isset($chapter_list_data[$value['parent_id']])){
						$chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
						unset($chapter_list[$key]);
					}
				}

				if($chapter_list AND $chapter_list_data){
					foreach ($chapter_list_data as $key => $value) {
						if(isset($value['sub'])){
							foreach ($value['sub'] as $k => $v) {
								foreach ($chapter_list as $k1 => $v1) {
									if($k == $v1['parent_id']){
										$chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
									}
								}
							}
						}
					}
				}
			}
			return $chapter_list_data;
		}
	}